package com.example.demo.utrl;

import com.aliyuncs.DefaultAcsClient;
import com.aliyuncs.IAcsClient;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsRequest;
import com.aliyuncs.dysmsapi.model.v20170525.SendSmsResponse;
import com.aliyuncs.exceptions.ClientException;
import com.aliyuncs.profile.DefaultProfile;
import com.aliyuncs.profile.IClientProfile;

public class SendSMSUtil {
    private static final String accessKeyId = "LTAI5tMQNXUTt9YaEoTZ9Qju";
    private static final String accessKeySecret = "CpaHl0o4ElSbeGENTzAhFfPHcFfjLD";
    private static final String endpoint = "dysmsapi.aliyuncs.com";
    private static final String TemplateCode = "SMS_478445530";//30通知短信，29验证码
    private static final String SignName = "山石飞讯";

    static String TemplateParam = "";
    static String conference = "第一届国际";
    static String address = "成都市高新区天府大道";
    static String time = "2020年12月12日";

    public static SendSmsResponse sendSms(String phoneNumber, String code) throws ClientException {

        if (TemplateCode.endsWith("30")) {
            TemplateParam = "{\"conference\":\"" + conference + "\"," +
                    "\"address\":\"" + address + "\"," +
                    "\"time\":\"" + time + "\"," +
                    "\"code\":\"" + code + "\"}";
        } else {
            TemplateParam = "{\"code\":\"" + code + "\"}";
        }

        IClientProfile profile = DefaultProfile.getProfile("cn-chengdu", accessKeyId, accessKeySecret);
        IAcsClient client = new DefaultAcsClient(profile);
        SendSmsRequest request = new SendSmsRequest();
        request.setPhoneNumbers(phoneNumber);
        request.setSignName(SignName);
        request.setTemplateCode(TemplateCode);
        request.setTemplateParam(TemplateParam);
        request.setEndpoint(endpoint);
        SendSmsResponse response = client.getAcsResponse(request);
        System.out.println("阿里云响应状态: " + response.getCode());
        System.out.println("阿里云响应消息: " + response.getMessage());
        return response;
    }
}